package driver.ui.driver;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;


import driver.ui.R;
import driver.ui.driver.Beans.notification;
import driver.ui.driver.Beans.pickupsetObject;
import Databasehelper.Notificationhelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.TimePicker;

public class Pickupsettings extends Activity{
	ListView pickList;
	TextView settingsName;
	PickupsetAdapter arrayAdapter;
	pickupsetObject set5;
	pickupsetObject set4;
	pickupsetObject set;
	pickupsetObject set2;
	pickupsetObject set3;
	String s="";
	String par="";
	int no;
	Boolean flagstatus;
	int startorend=0;
	private int mYear;
	private int mMonth;
	private int mDay;
    private int mHour;
    private int mMinute;

    static final int TIME_DIALOG_ID = 0;
	static final int DATE_DIALOG_ID = 1;
	
	Notificationhelper dbhelper;
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        setContentView(R.layout.pickupsettings);
	        dbhelper=new Notificationhelper(this);
	        Bundle extras = getIntent().getExtras();

	        
	        settingsName=(TextView)findViewById(R.id.settingname);
	        settingsName.setText(extras.getString("name"));
	        pickList = (ListView) findViewById(R.id.pickList);
	        ArrayList<pickupsetObject>  picklist= new ArrayList<pickupsetObject>();
	        flagstatus=false;
	        //Initialize our array adapter notice how it references the listitems.xml layout
	        arrayAdapter = new PickupsetAdapter(Pickupsettings.this, R.layout.listitems,picklist);

	        //This Page is built based on ListView
	        final AlertDialog.Builder shareBuilder=new AlertDialog.Builder(this);
	        final AlertDialog.Builder contactBuilder=new AlertDialog.Builder(this);
	        set = new pickupsetObject();
	        set.setOption("Date");
	        picklist.add(set);
	        
	        set2 = new pickupsetObject();
	        set2.setOption("Start Time");
	       	picklist.add(set2);
	       	
	       	 set3 = new pickupsetObject();
	        set3.setOption("End Time");
	       	picklist.add(set3);
	       	
	       	set4 = new pickupsetObject();
	        set4.setOption("Participants");
	       	picklist.add(set4);
	       	
	       	set5 = new pickupsetObject();
	        set5.setOption("Sharing");
	       	picklist.add(set5);
	       	
	       	
	        //Set the above adapter as the adapter of choice for our list
	        pickList.setAdapter(arrayAdapter);
	       

	        //ListView's Listener
	        pickList.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int position,
						long arg3) {
					switch(position)
					{
					//This is for Date Picker
					case 0:
					{
						
						 showDialog(DATE_DIALOG_ID);
						 break;

					}
					//This is for Time Picker
					case 1:
						startorend=1;
						showDialog(TIME_DIALOG_ID);
						break;
					case 2:
						startorend=2;
						showDialog(TIME_DIALOG_ID);
						break;
					
					//This is for choosing participants
					case 3:
					{
						final CharSequence[] Participants = {"Alice Jones", "Clarice Smith","David Johnson"
								,"Eugene Crow","Fred Schmidt","George Hayes","Hanah Chan"};
						no=Participants.length;
						contactBuilder.setCancelable(true);
						contactBuilder.setIcon(R.drawable.w);
						contactBuilder.setTitle("Choose Your Particpants");
						contactBuilder.setSingleChoiceItems(Participants, 0, new DialogInterface.OnClickListener ()
						{
							@Override

						    public void onClick(DialogInterface dialog, int id) {
								 
					            	  
				            		
						
							}
							

						});
						contactBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
				               public void onClick(DialogInterface dialog, int id) {
				            	   par=par+"Alice Jones";
				            	   
				            	   
				            	   set4.setValue(par);
				            	   
				            	   arrayAdapter.notifyDataSetChanged();
				            	  
				               
				               }
						})
				           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				               public void onClick(DialogInterface dialog, int id) {
				                    dialog.cancel();
				               }
				           });
						contactBuilder.show();       
						break;
					}
					//This is for "Sharing"
					case 4:
						final CharSequence[] StatusTime = {"Your Location", "Your Status"};
						
						shareBuilder.setCancelable(true);
						shareBuilder.setIcon(R.drawable.w);
						shareBuilder.setTitle("When you near the pickup point, share the following info:");
						shareBuilder.setSingleChoiceItems(StatusTime, 0, new DialogInterface.OnClickListener ()
						{
							@Override

						    public void onClick(DialogInterface dialog, int id) {
							}
					

						});
					    shareBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
				               public void onClick(DialogInterface dialog, int id) {
	
				            	   if(!flagstatus)
				            	   {			            	  
				            		   if(s.equals(""))
				            			   s=s+"location";
				            		   else
				            			   s=s+",location";
				            		   flagstatus=true;
				            	   }
				            	   else
				            	   {
				            		   
				            		   if(s.equals(""))
				            			   s=s+"Status";
				            		   else
				            			   s=s+",Status";
				            	   }
				            				   
				            		   
				            	   
				            	   set5.setValue(s);
				            	   
				            	   arrayAdapter.notifyDataSetChanged();
				           
				               }
				           })
				           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				               public void onClick(DialogInterface dialog, int id) {
				                    dialog.cancel();
				               }
				           });
					
						shareBuilder.show();
						break;
					
					}
					
				
					
				}});
	        // get the current date
	        final Calendar c = Calendar.getInstance();
	        mYear = c.get(Calendar.YEAR);
	        mMonth = c.get(Calendar.MONTH);
	        mDay = c.get(Calendar.DAY_OF_MONTH);
	        // get the current time
	        final Calendar c2 = Calendar.getInstance();
	        mHour = c2.get(Calendar.HOUR_OF_DAY);
	        mMinute = c2.get(Calendar.MINUTE);


	     
	    }
	    // updates the date in the TextView
	    private void updateDisplay() {
	    	StringBuilder sb=new StringBuilder();
            // Month is 0 based so add 1
            sb.append(mMonth + 1).append("-");
            sb.append(mDay).append("-");
            sb.append(mYear).append(" ");
	        set.setValue(sb.toString());
	        arrayAdapter.notifyDataSetChanged();
	            
	    }
	    //updates the time in the TextView
	    private void updateDisplay2() {
	        StringBuilder sb=new StringBuilder();
	        sb.append(pad(mHour)).append(":");
	        sb.append(pad(mMinute));
	        if(startorend==1)
	        {
	        set2.setValue(sb.toString());
	        startorend=0;
	        }
	        else if(startorend==2)
	        {
	        	set3.setValue(sb.toString());
	        	startorend=0;
	        }
	        arrayAdapter.notifyDataSetChanged();
	    }

	    private static String pad(int c) {
	        if (c >= 10)
	            return String.valueOf(c);
	        else
	            return "0" + String.valueOf(c);
	    }
	    // the callback received when the user "sets" the date in the dialog
	    private DatePickerDialog.OnDateSetListener mDateSetListener =
	            new DatePickerDialog.OnDateSetListener() {
	    	@Override
	                public void onDateSet(DatePicker view, int year, 
	                                      int monthOfYear, int dayOfMonth) {
	                    mYear = year;
	                    mMonth = monthOfYear;
	                    mDay = dayOfMonth;
	                    updateDisplay();
	                }};
	     @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                            mDateSetListener,
                            mYear, mMonth, mDay);
            case TIME_DIALOG_ID:
                return new TimePickerDialog(this,
                        mTimeSetListener, mHour, mMinute, false);
            }
            
            return null;
        }
	  // the callback received when the user "sets" the time in the dialog
	     private TimePickerDialog.OnTimeSetListener mTimeSetListener =
	         new TimePickerDialog.OnTimeSetListener() {
	    	 @Override
	             public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
	                 mHour = hourOfDay;
	                 mMinute = minute;
	                 updateDisplay2();
	             }

		
	         };

	    public void sendNotificationClicked(View view)
	    {
	    	notification not=new notification();
	    	not.setDate(set.getValue());
	    	not.setTime(set2.getValue()+"-"+set3.getValue());
	    	String[]names=settingsName.getText().toString().split(" ");
	    	not.setSentfrom(names[0]+names[1]);
	    	String[]usernames=set4.getValue().split(" ");
	    	not.setSentto(usernames[0]+usernames[1]);
	    	not.setShare(set5.getValue());
	    	not.setType("notification");
	    	dbhelper.insertNot(not);
			Intent addIntent=new Intent();
			addIntent.putExtra("name",settingsName.getText().toString());
			addIntent.setAction("Contactavailable");
			startActivity(addIntent);
	    }
	 
	
	 
	    }
	 
	       
	 
	 


